package com.wtwd.device.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.device.constant.RespCode;
import com.wtwd.device.model.entity.AlarmInfo;
import com.wtwd.device.model.vo.PageDataVO;
import com.wtwd.device.model.vo.RespEntity;
import com.wtwd.device.service.AlarmInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mjy
 * @since 2021-11-18
 */
//@Api(tags = "闹钟")
//@RestController
//@RequestMapping("/alarm-info")
public class AlarmInfoController {

    @Autowired
    private AlarmInfoService alarmInfoService;

    private static final Logger logger = LoggerFactory.getLogger(AlarmInfoController.class);

    /**
     * 根据设备id获取所有闹钟
     *
     * @param deviceId
     * @return
     */

    @ApiOperation(value = "根据设备id获取所有闹钟")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", required = true, dataType = "long")
    })
    @GetMapping("/getAllAlarmByDeviceId")
    public RespEntity<List<AlarmInfo>> getAllAlarmByDeviceId(@RequestParam("deviceId") Long deviceId) {
        logger.info("getAllAlarmByDeviceId param={}", deviceId);
        List<AlarmInfo> alarmInfos = alarmInfoService.getAllAlarmByDeviceId(deviceId);
        return new RespEntity<>(RespCode.SUCCESS, alarmInfos);
    }

    /**
     * 根据设备id获取所有闹钟并分页
     *
     * @param currentPage
     * @param pageSize
     * @param deviceId
     * @return
     */

    @ApiOperation(value = "根据设备id获取所有闹钟并分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "页数", required = true, dataType = "int")
            , @ApiImplicitParam(name = "pageSize", value = "页大小", required = true, dataType = "int")
            , @ApiImplicitParam(name = "deviceId", required = true, dataType = "long")

    })
    @GetMapping("/getPageAlarmByDeviceId")
    public RespEntity<PageDataVO<AlarmInfo>> getPageAlarmByDeviceId(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                                    @RequestParam("deviceId") Long deviceId) {
        logger.info("getPageAlarmByDeviceId currentPage={},pageSize={},deviceId={}", deviceId, pageSize, deviceId);
        Page<AlarmInfo> page = new Page<>(currentPage, pageSize);
        AlarmInfo alarmInfo = new AlarmInfo();
        alarmInfo.setDeviceId(deviceId);
        return new RespEntity(RespCode.SUCCESS, alarmInfoService.queryPage(alarmInfo, page));
    }

    /**
     * 添加闹钟
     *
     * @param alarmInfo
     * @return
     */
    @ApiOperation(value = "添加闹钟")
    @PostMapping("/addAlarm")
    public RespEntity<AlarmInfo> addAlarm(@RequestBody AlarmInfo alarmInfo) {
        logger.info("addAlarm param={}", alarmInfo.toString());
        Integer rest = 0;
        try {
            rest = alarmInfoService.save(alarmInfo);
        } catch (DuplicateKeyException e) {
            return new RespEntity<>(RespCode.AlARM_REPEAT);
        }
        return new RespEntity(rest > 0 ? RespCode.SUCCESS : RespCode.FAIL, alarmInfo);
    }

    /**
     * 更新闹钟
     *
     * @param alarmInfo
     * @return
     */
    @ApiOperation(value = "更新闹钟")
    @PutMapping("/updateAlarm")
    public RespEntity updateAlarm(@RequestBody AlarmInfo alarmInfo) {
        logger.info("updateAlarm param={}", alarmInfo.toString());
        if (alarmInfo.getId() == null) {
            return new RespEntity(RespCode.PARAMS_ERROR);
        }
        Integer rest = alarmInfoService.updateAlarm(alarmInfo);
        return new RespEntity(rest > 0 ? RespCode.SUCCESS : RespCode.FAIL);
    }


    /**
     * 删除闹钟
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除闹钟")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true, dataType = "long")
    })
    @DeleteMapping("/deleteAlarm")
    public RespEntity deleteAlarm(@RequestParam Long id) {
        logger.info("updateAlarm param={}", id);
        if (id == null) {
            return new RespEntity(RespCode.PARAMS_ERROR);
        }
        Integer rest = alarmInfoService.deleteAlarm(id);
        return new RespEntity(rest > 0 ? RespCode.SUCCESS : RespCode.FAIL);
    }


    /**
     * 批量更新闹钟状态
     */
    @ApiOperation(value = "批量更新闹钟的状态")
    @PostMapping("/bathUpdateAlarmState")
    public RespEntity<List<AlarmInfo>> bathUpdateAlarmState(@RequestBody List<AlarmInfo> alarmInfos) {
        logger.info("bathUpdateAlarmState param={}", alarmInfos.toString());
        if (alarmInfos == null || alarmInfos.size() == 0) {
            return new RespEntity(RespCode.PARAMS_ERROR);
        }
        Integer rest = alarmInfoService.bathUpdateAlarmState(alarmInfos);
        Long deviceId=null;
        for (AlarmInfo alarmInfo : alarmInfos) {
            if (alarmInfo.getDeviceId()!=null){
               deviceId=alarmInfo.getDeviceId();
               break;
            }
        }
        List<AlarmInfo> alarmInfoaAll = alarmInfoService.getAllAlarmByDeviceId(deviceId);
        return new RespEntity(rest > 0 ? RespCode.SUCCESS : RespCode.FAIL,alarmInfoaAll);
    }


}

